﻿using QiBu.Api.Helper.Common;
using QiBu.Api.Logic;
using QiBu.Api.Map;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace QiBu.Api.Areas.App.Controllers
{
    public class CalendarController : BaseController
    {
        //
        // GET: /App/Calendar/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取日程
        /// </summary>
        /// <param name="start">开始时间</param>
        /// <param name="end">截止时间</param>
        /// <returns></returns>
        public ContentResult AjaxGet(SYS_User user, string start = "", string end = "")
        {
            List<APP_Schedule> list = APP_ScheduleLogic.Instance.Datas.Where(x => x.userid == user.UserId).ToList();
            return Content(JSONHelper.Encode(list), "application/json");
        }

        /// <summary>
        /// 新增/编辑日程
        /// </summary>
        /// <param name="user">当前用户</param>
        /// <param name="ev">日程实体</param>
        /// <param name="daydiff">变动了多少天</param>
        /// <param name="minudiff">变动了多少分钟</param>
        /// <param name="type">编辑类型 0：更新整个实体 1：拖拽更新 2：更新截止时间</param>
        /// <returns></returns>
        public ActionResult AjaxEdit(SYS_User user, APP_Schedule ev, int daydiff = 0, int minudiff = 0, int type = 0)
        {
            bool flag = false;
            ev.userid = user.UserId;
            if (ev.end == null)
            {
                ev.end = ev.start.AddHours(24 - ev.start.Hour).AddSeconds(-1);//没有给结束时间的话就默认到今天截止
                if (ev.start.Minute == 30)
                {
                    ev.end = Convert.ToDateTime(ev.end).AddMinutes(-30);//要考虑拖的时间在一半的情况
                }
            }
            if (ev.id > 0)
            {
                if (type == 0)
                {
                    flag = APP_ScheduleLogic.Instance.Update(ev) > 0 ? true : false;
                }
                else if (type == 1)
                {
                    var model = APP_ScheduleLogic.Instance.Find(x => x.id == ev.id); //避免在前端复杂的逻辑交互，还是在这儿取出来再更新回去吧
                    model.start = model.start.AddDays(daydiff).AddMilliseconds(minudiff);
                    model.end = Convert.ToDateTime(model.end).AddDays(daydiff).AddMilliseconds(minudiff);
                    flag = APP_ScheduleLogic.Instance.Update(model) > 0 ? true : false;
                }
                else
                {
                    var model = APP_ScheduleLogic.Instance.Find(x => x.id == ev.id); //避免在前端复杂的逻辑交互，还是在这儿取出来再更新回去吧
                    model.end = Convert.ToDateTime(model.end).AddDays(daydiff).AddMilliseconds(minudiff);
                    flag = APP_ScheduleLogic.Instance.Update(model) > 0 ? true : false;
                }
            }
            else
            {
                flag = APP_ScheduleLogic.Instance.Insert(ev) > 0 ? true : false;
            }
            return Json(flag, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 删除日程
        /// </summary>
        /// <param name="id">日程ID</param>
        /// <returns></returns>
        public ActionResult AjaxDel(int id)
        {
            return Json(APP_ScheduleLogic.Instance.Delete(id), JsonRequestBehavior.AllowGet);
        }
    }
}
